home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / lib / mntlib44.zoo / mntlib / fstat.c < prev    next >
C/C++ Source or Header  |  1994-03-01  |  2KB  |  97 lines

  1. #include <limits.h>
  2. #include <types.h>
  3. #include <stat.h>
  4. #include <ctype.h>
  5. #include <errno.h>
  6. #include <osbind.h>
  7. #include <mintbind.h>
  8. #include <string.h>
  9. #include <time.h>
  10. #include <unistd.h>
  11. #include <support.h>
  12. #include <ioctl.h>    /* for FSTAT */
  13. #include "lib.h"
  14.  
  15. extern int __mint;
  16.  
  17. extern ino_t __inode;
  18.  
  19. __EXTERN int _do_stat __PROTO((const char *_path, struct stat *st, int lflag));
  20.  
  21. /* 
  22.  * fstat: if we're not running under MiNT, this is pretty bogus.
  23.  * what we can really find is:
  24.  * modification time: via Fdatime()
  25.  * file size: via Fseek()
  26.  * fortunately, these are the things most programs are interested in.
  27.  * BUG: passing an invalid file descriptor gets back a stat structure for
  28.  * a tty.
  29.  */
  30.  
  31. int
  32. fstat(fd, st)
  33. int fd;
  34. struct stat *st;
  35. {
  36.     long oldplace, r;
  37.     _DOSTIME timeptr;
  38.     short magic;
  39.  
  40.     if (__mint >= 9) {        /* use FSTAT Fcntl */
  41.         r = Fcntl(fd, (long)st, FSTAT);
  42.         if (r) {
  43.             errno = (int) -r;
  44.             return -1;
  45.         }
  46.         __UNIXTIME(st->st_mtime);
  47.         __UNIXTIME(st->st_atime);
  48.         __UNIXTIME(st->st_ctime);
  49.         st->st_blocks = (st->st_blocks * st->st_blksize) / 512;
  50.         return 0;
  51.     }
  52.  
  53.     r = Fdatime(&timeptr, fd, 0);
  54.     if (r < 0) {            /* assume TTY */
  55.         st->st_mode = S_IFCHR | 0600;
  56.         st->st_attr = 0;
  57.         st->st_mtime = st->st_ctime = st->st_atime =
  58.             time((time_t *)0) - 2;
  59.         st->st_size = 0;
  60.     } else {
  61.         st->st_mtime = st->st_atime = st->st_ctime =
  62.             _unixtime(timeptr.time, timeptr.date);
  63.         st->st_mode = S_IFREG | 0644;        /* this may be false */
  64.         st->st_attr = 0;            /* because this is */
  65.  
  66.     /* get current file location */
  67.         oldplace = Fseek(0L, fd, SEEK_CUR);
  68.         if (oldplace < 0) {        /* can't seek -- must be pipe */
  69.             st->st_mode = S_IFIFO | 0644;
  70.             st->st_size = 1024;
  71.         } else {
  72.             r = Fseek(0L, fd, SEEK_END);    /* go to end of file */
  73.             st->st_size = r;
  74.             (void)Fseek(0L, fd, SEEK_SET);    /* go to start of file */
  75.             /* check for executable file */
  76.             if (Fread(fd, 2, (char *)&magic) == 2) {
  77.                 if (magic == 0x601a || magic == 0x2321)
  78.                     st->st_mode |= 0111;
  79.             }
  80.             (void)Fseek(oldplace, fd, SEEK_SET);
  81.         }
  82.     }
  83.  
  84. /* all this stuff is likely bogus as well. sigh. */
  85.     st->st_dev = Dgetdrv();
  86.     st->st_rdev = 0;
  87.     st->st_uid = getuid();
  88.     st->st_gid = getgid();
  89.     st->st_blksize = 1024;
  90. /* note: most Unixes measure st_blocks in 512 byte units */
  91.     st->st_blocks = (st->st_size + 511) / 512;
  92.     st->st_ino = ++__inode;
  93.     st->st_nlink = 1;
  94.     return 0;
  95. }
  96.  
  97.